package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import database.DBconn;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JPasswordField;

public class LoginMenu extends JFrame {

	private JPanel contentPane;
	private JTextField usertxtField;
	private JPasswordField passtxtField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginMenu frame = new LoginMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginMenu() {
		setResizable(false);
		setTitle("Campus Instant Messaging");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 334, 624);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JPanel panel = new JPanel();
		panel.setBounds(0, 0, 318, 585);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblCampusInstantMessaging = new JLabel("Campus Instant Messaging");
		lblCampusInstantMessaging.setBounds(85, 33, 168, 16);
		panel.add(lblCampusInstantMessaging);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(37, 81, 65, 16);
		panel.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(37, 127, 64, 16);
		panel.add(lblPassword);
		
		JLabel label = new JLabel(":");
		label.setBounds(95, 69, 9, 41);
		panel.add(label);
		
		JLabel label_1 = new JLabel(":");
		label_1.setBounds(95, 108, 9, 54);
		panel.add(label_1);
		
		usertxtField = new JTextField();
		usertxtField.setBounds(139, 75, 134, 28);
		panel.add(usertxtField);
		usertxtField.setColumns(10);
		
		passtxtField = new JPasswordField();
		passtxtField.setBounds(139, 121, 134, 28);
		panel.add(passtxtField);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				new RegisterMenu(panel).setVisible(true);
				
			}
		});
		btnRegister.setBounds(31, 173, 117, 29);
		panel.add(btnRegister);
		
		JButton logButton = new JButton("Login");
		logButton.setBounds(158, 173, 117, 29);
		panel.add(logButton);
		logButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					String sql="SELECT * FROM userinfo";				
					DBconn.getConnection();
			        Statement stmt = DBconn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
			        ResultSet rset = stmt.executeQuery(sql);						            
		            String user=usertxtField.getText();
		            String pwd= new String (passtxtField.getPassword());
		            int tmp=0;
		            while(rset.next()) {
		            String uid=rset.getString("u_id");
		            String uname=rset.getString("u_username");		           
		            String password=rset.getString("u_password");
		            if ((user.equals(uname)) && (pwd.equals(password))){
		            			            	
		            	new ChatMenu(panel, uid).setVisible(true);
		                tmp++;
		            }
		            }
		            if (tmp==0) {
		            JOptionPane.showMessageDialog(null, "Username and Password not in database!");		            
		             }		            
		            
		        } catch (Exception e1) {
		            System.out.print(e1.getMessage());
		        }
				
			}
		});
		
		
	}
}
